package com.fooddelivery.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fooddelivery.Common.Result;
import com.fooddelivery.Common.queryParam.QueryPageParamCategory;
import com.fooddelivery.Entity.Category;
import com.fooddelivery.Service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import static com.fooddelivery.Common.Result.suc;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author zhengruiyang
 * @since 2025-08-31
 */
@RestController
@RequestMapping("/category")
public class CategoryController {



    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/list")
    public Result list() {
        return Result.suc(categoryService.list());
    }
    @PutMapping("/save")
    public boolean save(@RequestBody Category category) {
        return categoryService.saveOrUpdate(category);
    }
    @RequestMapping("/getById")
    public Result getById(@RequestBody Category category) {
        return Result.sucMsg("查询成功",categoryService.getById(category.getId()));
    }

    @DeleteMapping("/deleteById")
    public Result delete(@RequestBody Category category) {
         categoryService.removeById(category);
         return Result.sucMsg("删除成功");
    }

    @RequestMapping("/listPage")//类别的分类查询
    public Result listPage(@RequestBody QueryPageParamCategory  queryPageParamCategory){
        Page<Category> page = new Page();
        page.setCurrent(queryPageParamCategory.getPageNum());
        page.setSize(queryPageParamCategory.getPageSize());
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Category category = queryPageParamCategory.getCategory(); // 获取查询条件对象
        if (StringUtils.hasText(category.getName())) {
            lambdaQueryWrapper.like(Category ::getName, category.getName());
        }// 添加查询条件
        IPage result=categoryService.page(page,lambdaQueryWrapper);
        System.out.println(result.getTotal());
        System.out.println(result.getRecords().size());
        return suc(result.getRecords(), result.getTotal());
    }
}
